﻿Public Partial Class Booking_Bas_Vessel_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim ds As DataTable = clsGet.Get_WharfAll("B")
            If (ds.Rows.Count > 0) Then
                With ddlWharf
                    .Items.Clear()
                    .ClearSelection()
                    .DataTextField = "Wharf"
                    .DataValueField = "BookingWharf"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--กรุณาเลือก-->")
                    .Items(0).Value = "0"
                End With
            End If
            If (Not (Session("Edit_Bas") Is Nothing)) Then
                txtCompany.Text = Session("Edit_Bas").ToString.Split("$")(1)
                txtProductName.Text = Session("Edit_Bas").ToString.Split("$")(2)
                If Not (ddlWharf.Items.FindByValue(Session("Edit_Bas").ToString.Split("$")(3) & "") Is Nothing) Then
                    ddlWharf.SelectedValue = Session("Edit_Bas").ToString.Split("$")(3) & ""
                End If
                txtFender.Text = Session("Edit_Bas").ToString.Split("$")(4)
                ViewState("Edit_Bas") = Session("Edit_Bas").ToString.Split("$")(0)
            Else
                ViewState("Edit_Bas") = ""
            End If
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("Edit_Bas") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtCompany.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุชื่อ บริษัท');", True)
            Exit Sub
        End If
        If (txtProductName.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ สินค้า');", True)
            Exit Sub
        Else
            Dim product As DataTable = clsGet.Get_Product_no(txtProductName.Text)
            If product.Rows.Count <> 1 Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูลสินค้า !!');", True)
                Exit Sub
            End If
        End If
        If (ddlWharf.SelectedValue = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการเลือก Wharf');", True)
            Exit Sub
        End If
        If (txtFender.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Fender');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        Dim BV_no As String = ""
        If ViewState("Edit_Bas").ToString() = "" Then
            BV_no = clsDB.GeneratorID(conn, "Booking_Bas_Vessel")
        Else
            BV_no = ViewState("Edit_Bas").ToString()
        End If

        '  tran = clsDB.BeginTran(conn)
        If (clsFc.InsertBooking_Bas_Vessel(ViewState("Edit_Bas").ToString(), BV_no, txtCompany.Text, txtProductName.Text, ddlWharf.SelectedValue, txtFender.Text, conn, tran) > 0) Then
            Session("Edit_Bas") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "parent.closeModal();alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class